How do I get a Type[] with arguments from a MethodCallExpression?

Posted by Tomas Lycken on Stack Overflow See other posts from Stack Overflow or by Tomas Lycken
Published on 2010-05-02T22:20:26Z Indexed on 2010/05/02 22:28 UTC
Read the original article Hit count: 162

Filed under:
|
|
|

I'm reflecting over a class (in a unit test of said class) to make sure its members have all the required attributes. To do so, I've constructed a couple of helpers, that take an Expression as an argument. I do some checks for it, and take slightly different actions depending on what type of Expression it is, but it's basically the same.

Now, my problem is that I have several methods with the same name (but different signatures), and the following code throws an AmbiguousMatchException:

// TOnType is a type argument for the type where the method is declared
// mce is the MethodCallExpression
var m = typeof(TOnType).GetMethod(mce.Method.Name);

Now, if I could add an array of Type[] with the types of the arguments to this method as a second parameter to .GetMethod(), the problem would be solved.

But how do I find this Type[] array that I need?

I have cast the Expression<Func<...>> to an Expression, and then to a MethodCallExpression, and in this method the contents of <...> is not known.

© Stack Overflow or respective owner

Related posts about c#

Related posts about reflection